package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.domain.MarketAdExample;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.service.AdminAdService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author Meltedicecream
 * @since 2023/06/19 21:36
 */
public class AdminAdServiceImpl implements AdminAdService {
    @Override
    public List<MarketAd> list(Integer page, Integer limit, String sort, String order, String name, String content) {
        SqlSession session = MybatisUtil.getSession();
        MarketAdMapper mapper = session.getMapper(MarketAdMapper.class);
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();


        //判断name与content是否为空，空则返回所有数据，非空则添加where语句
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(content)){
            criteria.andContentLike("%" + content + "%");
        }
        // 在sql语句中加入排序规则
        marketAdExample.setOrderByClause(sort + " " + order);
        //使用分页插件
        PageHelper.startPage(page,limit);
        //返回广告集合
        List<MarketAd> adList = mapper.selectByExample(marketAdExample);
        session.commit();
        session.close();
        return adList;
    }
}
